package externalflame.systemdesign.newsfeed.dao;

import externalflame.systemdesign.newsfeed.entity.Tweet;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Mapper
public interface TweetDao {
    @Select({"<script>",
            "select id, user_id, content, created_time from tweet T",
            "where T.user_id = #{userId}\n",
            "<if test=\"maxId != null\">and id <![CDATA[<=]]> #{maxId}</if>",
            "limit #{pageSize}",
            "</script>"})
    List<Tweet> findTweets(@Param("userId") Long userId, @Param("maxId") Long maxId, @Param("pageSize") Long pageSize);

    @Insert("insert into tweet (id, user_id, content, created_time) values (#{id}, #{userId}, #{content}, #{createdTime})")
    void add(@Param("id") Long id, @Param("userId") Long userId, @Param("content") String content, @Param("createdTime") Date createdTime);

    @Select("select id, user_id, content, created_time from tweet where id = #{id}")
    Tweet getTweetById(@Param("id") Long id);


    @Select({
        "<script>",
        "select id, user_id, content, created_time from interest_tweet",
        "where viewer_id = #{viewerId}",
        "<if test=\"maxId != null\">and id <![CDATA[<=]]> #{maxId}</if>",
        "limit #{pageSize}",
        "</script>"
    })
    List<Tweet> findTweetsInInterestTweet(@Param("viewerId") Long viewerId, @Param("maxId") Long maxId, @Param("pageSize") Long pageSize);

    @Insert("insert into interest_tweet (id, user_id, content, created_time, viewer_id) values (#{id}, #{userId}, #{content}, #{createdTime}, #{viewerId})")
    void addToInterestTweet(@Param("id") Long id, @Param("userId") Long userId, @Param("content") String content, @Param("createdTime") Date createdTime, @Param("viewerId") Long viewerId);
}
